import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { NzModalRef, NzModalService } from 'ng-zorro-antd';
import { TeamManagerService} from "../my-service/teamManager.service";
import { UtilService } from '../my-service/util.service';

import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';


@Component({
  selector: 'nav-root',
  templateUrl: './nav.component.html',
  styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit{
  Cover='';
  defaultCover='/assets/img/image/datouxiang.png';
  activeMenu: string = 'home';
  confirmModal: NzModalRef;
  readImgUrl='';
  stuGuid:string;
  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private modal: NzModalService,
    private utilService: UtilService,
    private teamManagerService:TeamManagerService,
  ) {
    this.readImgUrl = this.utilService.readImgUrl;
  }
 getUserGuid() {
    this.utilService.getUserInfo().then(res=>{
      if (res.status ==1) {
        this.stuGuid  = res.userGuid;
        this.getUserInfo();
      }
    })
 }
 getUserInfo() {
    this.teamManagerService.getUserInfo(this.stuGuid).then(res=>{
      if(res.status == 1&&res.data){
          this.Cover = res.data.Cover;
      }
    })
 }
  ngOnInit() {
    this.getUserGuid();
    this.router.events
    .filter(event => event instanceof NavigationEnd)
    .map(() => this.activatedRoute)
    .map(route => {
      while (route.firstChild) route = route.firstChild;
      return route;
    })
    .filter(route => route.outlet === 'primary')
    .mergeMap(route => route.data)
  .subscribe(event => {
      this.activeMenu = event.menu;
    });
  }

  changeRouting(address) {
    this.router.navigate([address]);
  }

  goHome() {
    this.router.navigate(['/']);
  }
  goto(path){
    this.router.navigate([`${path}`]);
  }
  // 测试/示例方法，可删除
  getUserInfoTest() {
    this.utilService.getUserInfo().then( res => {
      console.log(res);
    });
  }

  logout() {
    this.utilService.comfirm('确认退出登录？', () => {
      this.utilService.logout().then( res => {
        if(res.status === 1) {
          this.utilService.remind('success', '退出成功！');
          window.location.reload();
        } else this.utilService.remind('error', '退出失败，请重试！');
      });
    });
  }

}
